package com.lvmama.controller.rhino.system;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.lvmama.common.enums.CommonType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lvmama.client.rhino.MenuClient;
import com.lvmama.common.constants.Constants;
import com.lvmama.common.result.Result;
import com.lvmama.common.utils.JSONConversionUtil;
import com.lvmama.pojo.base.Page;
import com.lvmama.pojo.dto.rhino.system.MenuDTO;
import com.lvmama.pojo.dto.rhino.system.MenuQueryDTO;
import com.lvmama.pojo.dto.rhino.system.MenuTreeNodeVO;
import com.lvmama.pojo.dto.rhino.system.MenuTreeVO;
import com.lvmama.pojo.entity.rhino.system.Menu;
import com.lvmama.pojo.entity.rhino.system.User;


/**
 * @author shenpeng
 * @since 2018年8月6日
 */
@Slf4j
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    MenuClient menuClient;

    @RequestMapping("/menuList")
    public String menuList(Model model, MenuQueryDTO query) {
        query.setPage(true);
        query.setLimit(query.getLimit());
        Result<Page<Menu>> page = menuClient.getMenuList(query);
        model.addAttribute("page", page.getData());
        return "/system/menuList";
    }

    @RequestMapping("/newMenu")
    public String newMenu(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User currentUser = (User) session.getAttribute("currentUser");
        List<Menu> menus = menuClient.getAllMenuListByUser(currentUser).getData();
        if (menus.size() > 0) {
            List<MenuTreeVO> menuTrees = menus.stream().map(MenuTreeVO::new).collect(Collectors.toList());
            String menusString = JSONConversionUtil.objToString(menuTrees);
            model.addAttribute("menus", menusString);
        }
        model.addAttribute("newtext", true);
        Menu menu = new Menu();
        menu.setIsShow(Constants.IS_SHOW);
        menu.setDelFlag(0);
        menu.setParent(new Menu());
        CommonType[] getIsShowType = CommonType.getIsShowType();
        CommonType[] getDelFlagType = CommonType.getDelFlagType();
        List<CommonType> isShowList = Arrays.asList(getIsShowType);
        List<CommonType> delFlagList = Arrays.asList(getDelFlagType);
        model.addAttribute("isShowList", isShowList);
        model.addAttribute("delFlagList", delFlagList);
        model.addAttribute("menu", menu);
        return "/system/menuForm";
    }

    @ResponseBody
    @RequestMapping("/parent")
    public MenuTreeNodeVO getParent(String name) {
        MenuTreeNodeVO vo = new MenuTreeNodeVO();
        MenuQueryDTO query = new MenuQueryDTO();
        query.setName(name);
        Result<Page<Menu>> result = menuClient.getMenuList(query);
        Menu menu = result.getData().getRows().get(0);
        vo.setMsg(menu);
        Result<List<String>> icons = menuClient.getMenuIcons();
        vo.setIcons(icons.getData());
        vo.setCode(Constants.ONE);
        return vo;
    }

    @RequestMapping("/modify")
    @ResponseBody
    public Result<Boolean> modifyMenu(MenuDTO menuDTO) {
        log.info("MenuController modifyMenu Start");
        try{
            if (StringUtils.isNotBlank(menuDTO.getId())) {
                menuClient.updateMenu(menuDTO);
            } else {
                menuClient.saveMenu(menuDTO);
            }
        }catch (Exception e){
            log.error("MenuController modifyMenu error:{}",e.getMessage());
        }
        log.info("MenuController modifyMenu end");
        return Result.success(true);
    }

    @RequestMapping("/edit")
    public String editMenu(Model model, String id) {
        Result<Menu> result = menuClient.getMenuById(id);
        model.addAttribute("menu", result.getData());
        model.addAttribute("newtext", false);
        CommonType[] getIsShowType = CommonType.getIsShowType();
        CommonType[] getDelFlagType = CommonType.getDelFlagType();
        List<CommonType> isShowList = Arrays.asList(getIsShowType);
        List<CommonType> delFlagList = Arrays.asList(getDelFlagType);
        model.addAttribute("isShowList", isShowList);
        model.addAttribute("delFlagList", delFlagList);
        return "/system/menuForm";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Result<Boolean> deleteMenu(String id) {
        return menuClient.deleteMenuById(id);
    }
}
